package com.example.chatplatform.service.impl;

import com.example.chatplatform.mapper.WebsocketMapper;
import com.example.chatplatform.service.WebsocketService;
import jakarta.websocket.server.ServerEndpointConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Service
public class WebsocketServiceImpl implements WebsocketService {

    @Autowired
    public WebsocketMapper websocketMapper;

    public void addCronyMessage(Integer userId, Integer toId, String message) {
        // 获取当前日期和时间
        LocalDateTime currentDateTime = LocalDateTime.now();
        // 定义日期和时间格式化器
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        // 格式化当前日期和时间为字符串
        String formattedDateTime = currentDateTime.format(formatter);
        websocketMapper.addCronyMessage(userId, toId, message, formattedDateTime);
    }

    @Override
    public List<Integer> GroupId(Integer userId) {
        return websocketMapper.GroupId(userId);
    }

    @Override
    public void addGroupMessage(Integer toId, Integer userId, String message) {
        // 获取当前日期和时间
        LocalDateTime currentDateTime = LocalDateTime.now();
        // 定义日期和时间格式化器
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        // 格式化当前日期和时间为字符串
        String formattedDateTime = currentDateTime.format(formatter);
        websocketMapper.addGroupMessage(toId, userId, message, formattedDateTime);
    }
}
